﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CanSWAT.SWAT
{
    /// <summary>
    /// corresponding to a swat folder
    /// </summary>
    class Model
    {
        private string _folder;
        private SnowRedistribution _snowRedistribution = null;
        private FrozenSoilParameterFile _frozenSoil = null;
        
        public Model(string folder)
        {
            if (!System.IO.Directory.Exists(folder)) return;
            _folder = System.IO.Path.GetFullPath(folder);

            _snowRedistribution = new SnowRedistribution(_folder);
            _frozenSoil = new FrozenSoilParameterFile(_folder + @"\" + FrozenSoilParameterFile.DEFAULT_NAME);
        }

        public string Folder
        {
            get { return _folder; }
        }

        public SnowRedistribution SnowRedistribution
        {
            get
            {
                return _snowRedistribution;
            }
        }

        public FrozenSoilParameterFile FrozenSoil
        {
            get
            {
                return _frozenSoil;
            }
        }

        /// <summary>
        /// Run SWAT Check in the model result folder
        /// </summary>
        /// <remarks>Copy swat_check.exe from CanSWAT interface instllation folder to the model folder</remarks>
        public void Check(SWATType type)
        {
            //see if swat_check.exe already there
            string swatCheckLocation = _folder + @"\swat_check.exe";
            if (!System.IO.File.Exists(swatCheckLocation))
            {
                //try to copy the file from CanSWAT interface installation folder
                string swatCheckLocation_install = CanSWAT.InstallationFolder + @"swat_check\" + type.ToString() + @"\swat_check.exe";
                if (!System.IO.File.Exists(swatCheckLocation_install))
                {
                    CanSWAT.showInformationWindow("Can't find " + swatCheckLocation_install + ". Please re-install CanSWAT interface.");
                    return;
                }

                System.IO.File.Copy(swatCheckLocation_install, swatCheckLocation);
            }

            if (System.IO.File.Exists(swatCheckLocation))
                System.Diagnostics.Process.Start(swatCheckLocation);
        }

        public void RunSWAT(string swatName)
        {
            string originalExeFile = CanSWAT.InstallationFolder + @"swat_exes\" + swatName;

            if (!System.IO.File.Exists(originalExeFile))
            {
                CanSWAT.showInformationWindow("Can't find " + originalExeFile);
                return;
            }

            System.Diagnostics.Process swatProcess = new System.Diagnostics.Process();
            swatProcess.StartInfo.FileName = originalExeFile;
            swatProcess.StartInfo.WorkingDirectory = _folder;
            swatProcess.Start();
        }

        /// <summary>
        /// run CanSWAT, don't copy exe any more.
        /// </summary>
        public void RunSWAT(SWATType type)
        {
            string swatName = "";
            switch (type)
            {
                case  SWATType.CanSWAT:
                    swatName = "CanSWAT.exe";
                    break;
                case SWATType.SWAT2009:
                    swatName = "SWAT2009.exe";
                    break;
                case SWATType.SWAT2012:
                    swatName = "SWAT2012.exe";
                    break;

            }
            string originalExeFile = CanSWAT.InstallationFolder + swatName;

            if (!System.IO.File.Exists(originalExeFile))
            {
                CanSWAT.showInformationWindow("Can't find " + originalExeFile);
                return;
            }

            System.Diagnostics.Process swatProcess = new System.Diagnostics.Process();
            swatProcess.StartInfo.FileName = originalExeFile;
            swatProcess.StartInfo.WorkingDirectory = _folder;
            swatProcess.Start();
        }
    }
}
